package com.elow.testffmpeg.Demo3;


import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


// 摄像头数据采集及图层绘制
public class CameraView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback
{
    // 源视频帧宽/高
    private int srcFrameWidth  = 640;
    private int srcFrameHeight = 480;

    // 数据采集
    private int curCameraIndex = 1;
    private Camera camera = null;
    // 视频帧共享存储回调接口
    private SurfaceHolder surfaceHolder;

    private SaveFrameCallback saveFrameCallback = null;
    private SaveFrameCallback YUVFrameCallback = null;

    public CameraView(Context context)
    {
        super(context);

        if (Camera.getNumberOfCameras() > 1)
        {
            curCameraIndex = 1;
        }
        else
        {
            curCameraIndex = 0;
        }

        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder sh)
    {
        stopCamera();
        startCamera(curCameraIndex);
    }

    @Override
    public void surfaceChanged(SurfaceHolder sh, int format, int width, int height)
    {


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder sh)
    {
        stopCamera();
    }

    // 根据索引初始化摄像头
    public void initCamera(int cameraIndex)
    {
        // 初始化并打开摄像头
        if (camera == null)
        {
            try
            {
                camera = Camera.open(cameraIndex);
            }
            catch (Exception e)
            {
                return;
            }

            Camera.Parameters params = camera.getParameters();
            if (params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
            {
                // 自动对焦
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            }

            try
            {
                params.setPreviewFormat(ImageFormat.NV21);
                params.setPreviewSize(srcFrameWidth, srcFrameHeight);
                camera.setParameters(params);

                params = camera.getParameters();
                params.setPreviewFpsRange(15*1000, 30*1000);
                camera.setParameters(params);
            }
            catch (Exception e)
            {
            }

            try
            {
                camera.setPreviewDisplay(surfaceHolder);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }

            camera.startPreview();
            camera.setPreviewCallback(this);
            camera.setDisplayOrientation(180);
        }
    }

    // 打开摄像头
    private void startCamera(int cameraIndex)
    {
        initCamera(cameraIndex);
    }

    // 停止并释放摄像头
    public void stopCamera()
    {
        if (camera != null)
        {
            camera.setPreviewCallback(null);
            camera.stopPreview();
            camera.release();
            camera = null;
        }

    }
    // 获取摄像头视频数据
    @Override
    public void onPreviewFrame(byte[] data, Camera camera)
    {
        synchronized (this)
        {
            if ( saveFrameCallback != null )
                saveFrameCallback.onSaveFrames(data,data.length);
            if ( YUVFrameCallback != null )
                YUVFrameCallback.onSaveFrames(data,data.length);
        }
    }
    // 保存视频帧
    public interface SaveFrameCallback
    {
        public void onSaveFrames(byte[] data, int length);
    }
    public void setSaveFrameCallback(SaveFrameCallback saveFrameCallback)
    {
        this.saveFrameCallback = saveFrameCallback;
    }
    public void setDarwYUVFrameCallback(SaveFrameCallback saveFrameCallback)
    {
        this.YUVFrameCallback = saveFrameCallback;
    }
}

